//
//  ExpressionView.m
//  Comma
//
//  Created by liuchang on 15/1/26.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "ExpressionView.h"
#import "GlobalMacro.h"
#import "ImageDB.h"
#import "ExpressionButton.h"

@interface ExpressionView ()<ExpressionButtonDelegate, UIScrollViewDelegate>

@end

@implementation ExpressionView

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.sizeOfImage = 35.0f;
        if (SCREEN_WIDTH == 320) {
            self.sideDistance = 13.5f;
            self.distance = 8.0f;
        }
        else if (SCREEN_WIDTH == 375) {
            self.sideDistance = 20.0f;
            self.distance = 15.0f;
        }
        else if (SCREEN_WIDTH == 414) {
            self.sideDistance = 24.5f;
            self.distance = 20.0f;
//            self.sizeOfImage = 52.5f;
        }
        else {
#warning 预留
        }
        self.height = _sizeOfImage * 3 + _distance * 3 + 30.0f;
        [self setFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, _height)];
        [self setBackgroundColor:DIVIDINGLINECOLOR];
        [self makeNormalView];
    }
    return self;
}

- (void)makeNormalView
{
    self.mainView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, _height)];
    [_mainView setDelegate:self];
    [_mainView setPagingEnabled:YES];
    [_mainView setShowsHorizontalScrollIndicator:NO];
    [_mainView setShowsVerticalScrollIndicator:NO];
    [self addSubview:_mainView];
    NSArray * imaArr = [ImageDB searchAllDic];
    self.numberOfImage = [imaArr count];
    self.numberOfPage = _numberOfImage / 21 + (_numberOfImage % 21 == 0 ? 0 : 1);
    [self.mainView setContentSize:CGSizeMake(_numberOfPage * SCREEN_WIDTH, 0)];
    for (int j = 0 ; j < _numberOfPage; j ++) {
        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(j * SCREEN_WIDTH, 0, SCREEN_WIDTH, _height)];
        for (int i = 0; i < (j + 1 == _numberOfPage ? _numberOfImage % 21 : 21); i++) {
            NSArray * newArr = [imaArr objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(j * 21, (j + 1 == _numberOfPage ? _numberOfImage % 21 : 21))]];
            ExpressionButton * button = [[ExpressionButton alloc] initWithDictionary:[newArr objectAtIndex:i] Frame:CGRectMake(_sideDistance + (i % 7) * (_sizeOfImage + _distance), _distance + (_sizeOfImage + _distance) * (i / 7), _sizeOfImage, _sizeOfImage)];
            button.delegate = self;
            [view addSubview:button];
        }
        [_mainView addSubview:view];
    }
    self.pageCon = [[UIPageControl alloc] init];
    [_pageCon addTarget:self action:@selector(pageConAction:) forControlEvents:UIControlEventValueChanged];
    [_pageCon setNumberOfPages:_numberOfPage];
    [_pageCon sizeToFit];
    [_pageCon setFrame:CGRectMake(0, _height - 20, SCREEN_WIDTH, 10)];
    [self addSubview:_pageCon];
}

- (void)expressionButtonAction:(NSDictionary *)dic
{
    [self.delegate expressionViewReturn:dic];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger number = scrollView.contentOffset.x / SCREEN_WIDTH;
    [_pageCon setCurrentPage:number];
}

- (void)pageConAction:(id)sender
{
    NSInteger number = _pageCon.currentPage;
    [_mainView setContentOffset:CGPointMake(number * SCREEN_WIDTH, 0)];
}

@end
